﻿using CQRS.Events;
using CQRS.Sample.Domain.BOs;
using CQRS.Sample.Domain.Commands;

namespace CQRS.Sample.Domain.CommandHandlers
{
    public class RegisterPlaneRepairCommandHandler : ICommandHandler<RegisterPlaneRepairCommand>
    {
        private readonly IDomainRepository<Event> _repository;

        public RegisterPlaneRepairCommandHandler(IDomainRepository<Event> repository)
        {
            _repository = repository;
        }

        #region ICommandHandler<RegisterPlaneCrashCommand> Members

        public void Execute(RegisterPlaneRepairCommand theEvent)
        {
            var repairedPlane = _repository.GetById<Plane>(theEvent.PlaneId);
            repairedPlane.PlaneRepaired(theEvent.PlaneId, theEvent.EffectuatedBy, theEvent.RepairDate,
                theEvent.ChangedPart, theEvent.RepairedPart);
            _repository.Add(repairedPlane);
            _repository.Commit();
        }

        #endregion
    }
}
